package co.yixiang.mall.biz.shop.mapper;

import co.yixiang.common.mproot.common.mapper.CoreMapper;
import co.yixiang.mall.common.shop.domain.YxStoreProductStockValue;
import co.yixiang.mall.common.shop.dto.YxStoreProductStockVo;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author zhuhongjian
 * @date 2020-05-12
 */
@Repository
public interface YxStoreProductStockValueMapper extends CoreMapper<YxStoreProductStockValue> {

    @Select("<script>SELECT\n" +
            "\t(@i:=@i+1) as seq,\n" +
            "\tstock.id sid,\n" +
            "\tproduct.store_name productName,\n" +
            "\tattr.sku sku,\n" +
            "\tCOALESCE(stock.purchase_volume,0) purchaseVolume,\n" +
            "\tCOALESCE(attr.price,0) purchasePrice,\n" +
            "\tCOALESCE(stock.purchase_volume*attr.price,0)  purchaseAmount,\n" +
            "\tCOALESCE(stock.purchase_volume-stock.stock,0) salesVolume,\n" +
            "\tCOALESCE(attr.sales,0) salesPrice,\n" +
            "\tCOALESCE((stock.purchase_volume-stock.stock)*attr.sales,0) salesAmount,\n" +
            "\tCOALESCE(stock.stock,0) lastVolume,\n" +
            "\tCOALESCE(attr.price,0) \tlastPrice,\n" +
            "\tCOALESCE(stock.stock*attr.price,0)  lastAmount\n" +
            "FROM\n" +
            "\tyx_store_product_stock_value stock\n" +
            "\tleft join yx_system_store store on store.id = stock.storeId\n" +
            "\tleft join yx_store_product product on product.id = stock.productId\n" +
            "\tleft join yx_store_product_attr_value attr on attr.id = stock.skuId\n" +
            "\t,(select @i:=0) as it" +
            " where 1=1 " +
            " <if test=\"productName!=null and productName!='' \"> and product.store_name like '%${productName}%' </if>" +
            " <if test=\"skuName!=null and skuName!='' \"> and attr.sku like '%${skuName}%'  </if>" +
            " <if test=\"id !=null and id!='' \"> and store.id = #{id} </if>" +
            " order by stock.id asc </script>")
    List<YxStoreProductStockVo> selectList(Map<String, Object> params);
}
